//
//  TTEmoticoncollection.swift
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/29.
//  Copyright © 2016年 陶路路. All rights reserved.
//

import UIKit

class TTEmoticoncollection: UICollectionView {

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI(){
        backgroundColor = UIColor.whiteColor()
        let layout = self.collectionViewLayout as? UICollectionViewFlowLayout
        registerClass(TTEmoticoncollectionCell.self, forCellWithReuseIdentifier: "cell")
        layout?.itemSize = CGSizeMake(SCREENW, 216 - 37)
        layout?.minimumInteritemSpacing = 0
        layout?.minimumLineSpacing = 0
        layout?.scrollDirection = .Horizontal
        dataSource = self
    }
}

extension TTEmoticoncollection:UICollectionViewDataSource{
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        return TTEmotionTools.sharedEmotionTools.emotionSection.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        let emotionCount = TTEmotionTools.sharedEmotionTools.emotionSection[section]
        
        return emotionCount.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! TTEmoticoncollectionCell
        let emotionCount = TTEmotionTools.sharedEmotionTools.emotionSection[indexPath.section]
        let emotionArray = emotionCount[indexPath.row]
        cell.emotionArray = emotionArray
        return cell
    }
}

class TTEmoticoncollectionCell:UICollectionViewCell {
    
    var indexPath:NSIndexPath?{
        
        didSet{
            
            lable.text = "第\(indexPath!.section)组\n第\(indexPath!.item)业"
        }
    }
    
    var emotionArray:[TTEmoticonsModel]?{
        
        didSet{
            
            for button in buttonArray{
                
                button.hidden = true
            }
            
            for (i,value) in emotionArray!.enumerate(){

                buttonArray[i].hidden = false
                (buttonArray[i] as! TTEmotionButton).buttonModel = value;
//           表示是 emoji 表情
                if value.type == "1"{
                    buttonArray[i].setTitle((value.code ?? "" as NSString).emoji(), forState: UIControlState.Normal)
                    buttonArray[i].titleLabel?.font = UIFont.systemFontOfSize(35)
                    buttonArray[i].setImage(nil, forState: UIControlState.Normal)
                    
                }else{
                    
//                    buttonArray[i].setImage(getImageWithBoundle(value.path), forState: UIControlState.Normal)
                    buttonArray[i].setImage(UIImage.getImageWithBoundle(value.path), forState:UIControlState.Normal)
                    buttonArray[i].setTitle(nil, forState: UIControlState.Normal)
                }
            }
        }
    }
    
    var buttonArray:[UIButton] = [UIButton]()
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setUpUI(){
        
        addSubview(lable)
        addSubview(deletButton)
        lable.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(contentView)
        }
        
        let width:CGFloat = SCREENW / 7
        let hight:CGFloat = self.bounds.size.height / 3
        
        for i in 0..<20{
            
            let col = i / 7
            let wol = i % 7
            let button:TTEmotionButton = TTEmotionButton()
            button.frame = CGRectMake(CGFloat(wol) * width, hight * CGFloat(col) , width, hight)
            button.addTarget(self, action: "didEmotionButton:", forControlEvents: UIControlEvents.TouchUpInside)
            addSubview(button)
            buttonArray.append(button)
        }
        
        deletButton.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(buttonArray.last!)
            make.leading.equalTo(buttonArray.last!.snp_trailing)
        }
        
    }
    
    lazy var lable:UILabel = {
       let text = UILabel()
        text.font = UIFont.systemFontOfSize(30)
        text.textAlignment = .Center
        text.numberOfLines = 0
        return text
    }()
    
    lazy var deletButton:TTEmotionButton = {
       
        let delet = TTEmotionButton()
        delet.setBackgroundImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        delet.setBackgroundImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        delet.addTarget(self, action: "didDeleteButton", forControlEvents: UIControlEvents.TouchUpInside)
        return delet
    }()
    
    func didEmotionButton(sender:TTEmotionButton){
        
        NSNotificationCenter.defaultCenter().postNotificationName("didEmotionButton", object: sender.buttonModel)
        print("didEmotionButton")
    }
    
    func didDeleteButton(){
        
        NSNotificationCenter.defaultCenter().postNotificationName("didDeleteButton", object: nil)
        print("deldteButton")
    }
    
    
// 获取bundle文件里的图片
    
    func getImageWithBoundle(path:String?)->UIImage?{
        
        return UIImage(named: path ?? "", inBundle: TTEmotionTools.sharedEmotionTools.emotionBundle, compatibleWithTraitCollection: nil)
    }
}
